<template>
  <div>
    <div class="goods">
      <div class="menu-wrapper" ref="menuWrapper">
        <ul>
          <li
            v-for="(item,index) in goods"
            class="menu-item"
            :class="{'current':currentIndex===index}"
            @click="selectMenu(index,$event)"
            ref="menuList"
          >
            <span class="text border-1px">
              <span v-show="item.type>0" class="icon" :class="classMap[item.type]"></span>
              {{item.name}}
            </span>
          </li>
        </ul>
      </div>
      <div class="foods-wrapper" ref="foodsWrapper">
        <ul>
          <li v-for="item in goods" class="food-list" ref="foodList">
            <h1>
              <span class="title">{{item.name}}</span>
            </h1>
            <ul>
              <li
                @click="selectFood(food,$event)"
                v-for="food in item.foods"
                class="food-item border-1px"
              >
                <div class="icon">
                  <img width="57" height="57" :src="food.icon">
                </div>
                <div class="content">
                  <h2 class="name">{{food.name}}</h2>
                  <p class="desc">{{food.description}}</p>
                  <div class="extra">
                    <span class="count">月售{{food.sellCount}}份</span>
                    <span>好评率{{food.rating}}%</span>
                  </div>
                  <div class="price">
                    <span class="now">￥{{food.price}}</span>
                    <span class="old" v-show="food.oldPrice">￥{{food.oldPrice}}</span>
                  </div>
                  <div class="cartcontrol-wrapper">
                    <cart-control :food="food" @add="onAdd"></cart-control>
                    
                  </div>
                </div>
              </li>
            </ul>
          </li>
        </ul>
      </div>

      <shop-cart
      ref="shopcart"
        :selectFoods="selectFoods"
        :deliveryPrice="seller.deliveryPrice"
        :minPrice="seller.minPrice"
      ></shop-cart>
    </div>
    <!-- <food @add="addFood" :food="selectedFood" ref="food"></food> -->
  </div>
</template>
<script>
import BScroll from "better-scroll";
import CartControl from "@/components/cartcontrol/app";
import ShopCart from "@/components/shopcart/app";
import { SellerService } from "@/api";
import { ERR_OK } from "@/utils/config";
export default {
  props: {
    seller: {
      type: Object
    }
  },
  data() {
    return {
      listHeight: [],
      goods: [],
      scrollY: 0,
      selectedFood: {}
    };
  },
  computed: {
    currentIndex() {
      for (let i = 0; i < this.listHeight.length; i++) {
        let height1 = this.listHeight[i];
        let height2 = this.listHeight[i + 1];
        if (!height2 || (this.scrollY >= height1 && this.scrollY < height2)) {
          //  this._followScroll(i);
          return i;
        }
      }
      return 0;
    },
    selectFoods() {
      let foods = [];
      this.goods.forEach(good => {
        good.foods.forEach(food => {
          if (food.count) {
            foods.push(food);
          }
        });
      });
      return foods;
    }
  },
  components: {
    CartControl,
    ShopCart
  },
  methods: {
    //获取分类
    getGoods() {
      SellerService.list().then(res => {
        let result = res.data;
        if (result.errno === ERR_OK) {
          this.goods = result.goods;
          console.log("res11", this.goods);
          this.$nextTick(() => {
            this._initScroll();
            this._calculateHeight();
          });
        }
      });
    },
    onAdd(target) {
      console.log('0',target,this.$refs.shopcart)
        this.$refs.shopcart.drop(target)
      },
    selectMenu(index, event) {
      // this.listHeight[index]
      if (!event._constructed) {
        return;
      }
      console.log("index", index);
      //this.currentIndex = index
      console.log("tes", this.scrollY);
      //this.scrollY = this.listHeight[index]
      //console.log('this.scrollY',this.scrollY)
      let foodList = this.$refs.foodList;
      let el = foodList[index];
      this.footScroll.scrollToElement(el, 300);
      //if()
    },
    selectFood() {
      console.log("5");
    },
    _initScroll() {
      console.log("xxxxxx");
      this.memuScroll = new BScroll(this.$refs.menuWrapper, { click: true });
      this.footScroll = new BScroll(this.$refs.foodsWrapper, {
        click: true,
        probeType: 3
      });
      this.footScroll.on("scroll", pos => {
        this.scrollY = Math.abs(Math.round(pos.y));
        console.log("pos", this.scrollY, this.listHeight);
      });
    },

    _calculateHeight() {
      let foodList = this.$refs.foodList;
      console.log("8", foodList);
      let height = 0;
      this.listHeight.push(height);
      for (let i = 0; i < foodList.length; i++) {
        let item = foodList[i];
        height += item.clientHeight;
        this.listHeight.push(height);
        console.log("height", this.listHeight);
      }
    }
  },
  created() {
    this.classMap = ["decrease", "discount", "special", "invoice", "guarantee"];
    this.getGoods();
  },
  mounted() {}
};
</script>
<style lang="scss">
@import "~@/assets/scss/mixin";
@import "~@/assets/scss/variable.scss";
@import "./style.scss";
</style>

